<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="query.keyword" placeholder="请输入标签名进行搜索" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      style="margin-top: 20px;"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="{row}">
          {{ row.username }}
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center">
        <template slot-scope="{row}">
          <el-image style="width: 60px" :preview-src-list="[row.avatar_url]"
                    :src="(row.avatar_url | getImage) || defaultAvatar"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center">
        <template slot-scope="{row}">
          {{ row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center">
        <template slot-scope="{row}">
          {{ row.email }}
        </template>
      </el-table-column>
      <el-table-column label="审批状态" class-name="status-col" align="center" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.register_status | statusFilter">
            {{ row.register_status === 0 ? '审批中' : row.register_status === 1 ? '已通过' : '未通过' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="150">
        <template slot-scope="{row}">
          <span
          >{{ row.createAt | dayFormatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="small" @click="handleChange(row)">编辑</el-button>
          <el-button type="info" size="small" @click="handleUpdate(row)">审批</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="text-align: right"
      v-show="total>0"
      :total="total"
      :page.sync="query.offset"
      :limit.sync="query.limit"
      @pagination="getList"
    />

    <!-- 编辑 -->
    <el-dialog :visible.sync="dialogUpdateVisible" title="编辑">
      <el-form :model="temp" label-width="80px" label-position="left">
        <el-form-item label="权威系数" prop="name">
          <el-input v-model="temp.cr" placeholder="请输入专家权威系数"/>
        </el-form-item>
        <el-form-item label="专家类型">
          <el-select @change="typeChange" v-model="labelValue" multiple placeholder="请选择">
            <el-option
              v-for="item in labelList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdateVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <!-- 审核 -->
    <el-dialog :visible.sync="dialogFormVisible" :title="textMap[dialogStatus]">
      <el-form ref="dataForm" :model="temp" label-width="80px" label-position="left">
        <el-form-item label="用户名">
          <span>{{ temp.username }}</span>
        </el-form-item>
        <el-form-item label="联系方式">
          <span>{{ temp.phone }}</span>
        </el-form-item>
        <el-form-item label="真实姓名">
          <span>{{ temp.name }}</span>
        </el-form-item>
        <el-form-item label="邮箱">
          <span>{{ temp.email || '无' }}</span>
        </el-form-item>
        <el-form-item label="身份证号">
          <span>{{ temp.id_number || '无' }}</span>
        </el-form-item>
        <el-form-item label="专业">
          <span>{{ temp.major || '无' }}</span>
        </el-form-item>
        <el-form-item label="学历">
          <span>{{ temp.education || '无' }}</span>
        </el-form-item>
        <el-form-item label="职称">
          <span>{{ temp.professional || '无' }}</span>
        </el-form-item>
        <el-form-item label="特长">
          <span>{{ temp.specialty || '无' }}</span>
        </el-form-item>
        <el-form-item label="工作年限">
          <span>{{ temp.work_year || '无' }}</span>
        </el-form-item>
        <el-form-item label="单位">
          <span>{{ temp.company || '无' }}</span>
        </el-form-item>
        <el-form-item label="权威系数">
          <span>{{ temp.cr || '无' }}</span>
        </el-form-item>
        <el-form-item label="材料证明">
          <template v-for="item in temp.images">
            <el-image :src="item" :preview-src-list="temp.images" style="width: 150px;margin: 5px"></el-image>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="reject">
          驳回
        </el-button>
        <el-button type="primary" @click="pass">
          通过
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userList, registerStatus, expertType, updateUser } from '@/api/user'
import { fetchList, update } from '@/api/label'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'

export default {
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'gray',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      total: 0,
      query: {
        limit: 10,
        offset: 1,
        keyword: '',
        permission: 2
      },
      textMap: {
        update: '详情',
        create: '新增'
      },
      dialogFormVisible: false,
      dialogUpdateVisible: false,
      dialogStatus: '',
      list: null,
      listLoading: true,
      temp: {
        id: '',
        cr: ''
      },
      labelList: [], // select选项
      labelValue: [], // select选中的值
      defaultAvatar: 'https://img0.baidu.com/it/u=705667404,1537851692&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'
    }
  },
  created() {
    this.getList()
    this.getLabelList()
  },
  methods: {
    handleFilter() {
      this.query.offset = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      userList(this.query).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 获取标签列表
    getLabelList() {
      fetchList({
        limit: 1000,
        offset: 1,
        keyword: ''
      }).then(res => {
        this.labelList = res.data.list
      })
    },
    // select值改变时保存类型
    typeChange() {
      expertType({ id: this.temp.id, type: this.labelValue.toString() }).then(res => {
        this.getList()
      })
    },
    // 编辑
    handleChange(row) {
      this.temp = Object.assign({}, row) // copy obj
      // 将'['1','2','3']'变成number数组
      if (this.labelValue) {
        this.labelValue = this.temp.expert_type?.replace(/\[|]/g, '').split(',').map(Number)
      } else {
        this.labelValue = []
      }
      this.dialogUpdateVisible = true
    },
    updateData() {
      const tempData = {
        id: '',
        cr: ''
      }
      tempData.id = this.temp.id
      tempData.cr = this.temp.cr
      updateUser(tempData).then(() => {
        this.getList()
        this.dialogUpdateVisible = false
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1000
        })
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.images = this.temp.materials_images.split(',')
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      if (this.$refs['dataForm']) {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    },
    // 通过
    pass() {
      registerStatus({ id: this.temp.id, status: 1 }).then(res => {
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 1000
        })
        this.dialogFormVisible = false
        this.getList()
      })
    },
    // 驳回
    reject() {
      registerStatus({ id: this.temp.id, status: 2 }).then(res => {
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 1000
        })
        this.dialogFormVisible = false
        this.getList()
      })
    }
  }
}
</script>
